

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author marduke
 */
import java.io.*;
import java.net.*;
import java.util.Enumeration;
import nanoxml.XMLElement;

public class Cliente {

    Socket requestSocket;
    ObjectOutputStream out;
    ObjectInputStream in;
    String message;
    String servidor = "127.0.0.1";
    String messageToServer;
    int puerto = 2004;

    Cliente() {
    }

    Cliente(String servidor, int puerto, String message) {
        this.servidor = servidor;
        this.puerto = puerto;
        this.messageToServer = message;

    }

    void run() {
        try {
            //1. creating a socket to connect to the server
            requestSocket = new Socket(servidor, puerto);
            System.out.println("Connected to server");
            //2. get Input and Output streams
            out = new ObjectOutputStream(requestSocket.getOutputStream());
            out.flush();
            in = new ObjectInputStream(requestSocket.getInputStream());
            //3: Communicating with the server
            do {
                try {
                    message = (String) in.readObject();
                    System.out.println("server>" + message);
                    sendMessage(messageToServer);

                    
                    message = (String) in.readObject();
                    imprimirResultado(message);
                    message = "bye";
                    
                        sendMessage(message);
                    


                } catch (ClassNotFoundException classNot) {
                    System.err.println("data received in unknown format");
                }
            } while (!message.equals("bye"));
        } catch (UnknownHostException unknownHost) {
            System.err.println("El servidor no esta activado, o el ip no es el correcto.!");
//        } catch (ConnectException connectException) {
//            System.err.println("Estas tratando de conectarte a un puerto malo!");
//            System.exit(-2);
//        }
        }
//         catch (SocketException socketEx) {
//            System.out.println("Se perdio la conexion con el servidor, Vamos a intentar otra vez.");
//            this.run();
//        }
        catch (IOException ioException) {
            ioException.printStackTrace();
        } finally {
            //4: Closing connection
            try {
                in.close();
                out.close();
                requestSocket.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

    }

    void sendMessage(String msg) {
        try {
            out.writeObject(msg);
            out.flush();
            System.out.println("client>" + msg);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    void imprimirResultado(String messageServidor) {
        XMLElement xml = new XMLElement();
        xml.parseString(messageServidor);

        if(xml.getName().equals("visitados")) {
            System.out.println("Los nodos alcanzables son:");
            Enumeration childs = xml.enumerateChildren();
            while (childs.hasMoreElements()) {
                XMLElement child =(XMLElement) childs.nextElement();
                System.out.println(child.getAttribute("hostaddress"));
            }
        }


    }
}
